$(function(){
	
	var page=1;
	var tddata=[
		{
			key:'company_name',
			type:1,
			url:''
		},{
			key:'short_name',
			type:1,
			url:'{:url("detail")}',
			id:137
		},{
			key:'code',
			type:1,
			url:''
		},{
			key:'account',
			type:1,
			url:''
		},{
			key:'linkman',
			type:1,
			url:''
		},{
			key:'phone',
			type:1,
			url:''
		},{
			key:'address',
			type:1,
			url:''
		},{
			key:'law_type2',
			type:1,
			url:''
		}
//		,{
//			key:'is_limit',
//			type:1,
//			url:''
//		}
		];
	
	var addoperation=``;
	var fields='';
	
	var data={
		page:1,
		pagenum:10
	}
	
	var where_json={}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	
	
	data.where_json=JSON.stringify(where_json)
	ajaxpassenger(data)
	function ajaxpassenger(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:'{:url("adminapi/Company/companyList")}',
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
				for(var i=0;i<res.data.length;i++){
					if(res.data[i].law_type==1){
						res.data[i].law_type2='非行政执法'
					}else{
						res.data[i].law_type='行政执法'
					}
					
					
					 
				}
//				$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation, 'driver_id'))

				layui.use('table', function(){
			  	var table = layui.table;
			  
			  //展示已知数据
			 	 table.render({
			    	elem: '#company'
				    ,cols: [[ //标题栏
				      {field: 'company_name', title: '公司名称', width: 250, sort: true,fixed: 'left'}
				      ,{field: 'short_name', title: '公司简称', width: 150}
				      ,{field: 'code', title: '公司代码', minWidth: 150}
				      ,{field: 'account', title: '登陆账号', minWidth: 150}
				      ,{field: 'linkman', title: '联系人', width: 180}
				      ,{field: 'phone', title: '联系电话', minWidth: 150}
				      ,{field: 'address', title: '地址', minWidth: 150}
				      ,{field: 'law_type2', title: '行政执法', width: 150}
				     
				    ]]
				    ,data:res.data
				    //,skin: 'line' //表格风格
				    ,even: true
				    //,page: true //是否显示分页
				    //,limits: [5, 7, 10]
				    //,limit: 5 //每页默认显示的数量
			  	});
				$('.pagination').html(pagechang1(res.count, 10, page))
				$('.laquo').on('click',function(){
					console.log(data)
					page-=1;
					data.page=page
					ajaxpassenger(data)
				})
				$('.pageli').on('click',function(){
					console.log(data)
					page=$(this).html()
					data.page=page
					ajaxpassenger(data)
				})
				//下一页
				$('.raquo').on('click',function(){
					console.log(data)
					page+=1;
					data.page=page
					ajaxpassenger(data)
				});
				})
				$('.daochu').on('click',function(){
						var getkey="";
	//					for(var key in res.description){
	//						getkey+=`<li>
	//									<label class="am-checkbox">
	//								      <input type="checkbox" value="" data-key='${key}' checked> ${res.description[key].name}
	//								    </label>
	//								</li>`
	//					}
						
	
						for(var iii=0;iii<tddata.length;iii++){
							if(tddata[iii].key=='law_type2'){
								for(var key in res.description){
									if(key=='law_type'){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='law_type' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}else{
								for(var key in res.description){
									if(key==tddata[iii].key){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='${tddata[iii].key}' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}
							
						}
						layer.open({
						  	type: 1,
						  	title:false,
						  	skin: 'layui-layer-rim', //加上边框
						  	area: ['300px', '400px'], //宽高
						  	content: `<div>
						 				<div style='height:300px;overflow:auto;margin-bottom:30px'>
									  	<ul class='daochuul'>${getkey}</ul>
									  	</div>
									  	<div style='text-align:center'>
									  		<button class='am-btn am-btn-sm am-btn-primary am-radius daochu_ok' type='button'>确定导出</button>
									  		<button class='am-btn am-btn-sm am-btn-primary am-radius' type='button'>取消</button>
									  	</div>
									</div>`,
							success:function(){
								var ddata={}
								$('.daochu_ok').on('click',function(){
									ddata.fields="";
									var where_jsondao={};
									{if $Think.get.company_name}
										where_jsondao.company_name=['like','%{$Think.get.company_name}%']
									{/if}
									
									
									ddata.where_json=JSON.stringify(where_jsondao)
									
									$('.daochuul li input[type="checkbox"]').each(function(){
										if(this.checked){
											ddata.fields+=($(this).data('key')+",")
										}
									})
									ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
									console.log(ddata.fields)
									console.log(`{:url("adminapi/Company/downLoadCompanyList")}?where_json=${ddata.where_json}&fields=${ddata.fields}`)
									window.open(`{:url("adminapi/Company/downLoadCompanyList")}?where_json=${ddata.where_json}&fields=${ddata.fields}`)
	//								
								})
								
							}
						});
					})
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
})
